<?php
namespace app\system\controller;

use app\system\model\Config as ConfigModel;
use think\facade\View;

/**
 * @title   配置
 * @desc    配置
 * @author  橘子味的心
 * @version 1.0
 */
class Config extends Base
{
    /**
     * @title   配置列表
     * @desc    配置列表
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   varchar 字段 &nbsp; 字段名 NO
     * @param   int 字段 &nbsp; 字段名 YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function lst()
    {
        if (request()->isPost()) {
            $map = [];
            $key = input('key');
            $type = input('type');
            $group = input('group');
            $limit = input('limit', 10);
            $field = input('field'); //字段
            $order = input('post.order'); //排序方式
            if (isset($type) && $type != "") {
                $map['type'] = $type;
            }

            if (isset($group) && $group != "") {
                $map['group'] = $group;
            }

            if (isset($key) && $key != "") {
                $map['title|name'] = ['like', "%" . $key . "%"];
            }

            if ($field && $order) {
                $od = $field . " " . $order;
            } else {
                $od = "create_time desc";
            }

            $config = new ConfigModel;
            $res = $config->getAll($map, $limit, $od);
            if (empty($res)) {
                return tocode(100);
            }
            return tocode(0, 'ok', $res->items(), $res->total());
        }
        $type = config_group('config_type_list');
        $group = config_group('config_group_list');
        return view('', ['type' => $type, 'group' => $group]);
    }
    /**
     * @title   验证配置标识的唯一性
     * @desc    验证配置标识的唯一性
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   int id &nbsp; ID YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function check()
    {
        extract(input());
        $config = new ConfigModel();
        if (isset($id) && $id != "") {
            $uid = $id;
        } else {
            $uid = '';
        }
        $config->check($name, $uid);
    }
    /**
     * @title   添加配置
     * @desc    添加配置
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   int id &nbsp; ID YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function add()
    {
        $config = new ConfigModel();
        if (request()->isPost()) {
            $data = input('post.');
            if (empty($data)) {
                return tocode(100, '数据不能为空');
            }
            $info = $config->info($data);
            if (!$info) {
                return tocode(100, '添加失败');
            }
            return tocode(200, '添加成功！');
        }
        $type = config_group('config_type_list');
        $group = config_group('config_group_list');
        return view('info', ['type' => $type, 'group' => $group]);
    }
    /**
     * @title   编辑配置
     * @desc    编辑配置
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   int id &nbsp; ID YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function edit()
    {
        $config = new ConfigModel();
        if (request()->isAjax()) {
            $id = input('id');
            if (empty($id)) {
                return tocode(100, 'ID不能为空');
            }
            if (request()->isPost()) {
                $data = input('post.');
                if (empty($data)) {
                    return tocode(100, '数据不能为空');
                }
                $info = $config->info($data);
                if (!$info) {
                    return tocode(100, '编辑失败');
                }
                return tocode(200, '编辑成功！');
            }
            $res = $config->getOne($id);
            return tocode(200, 'ok', $res);
        }
        $type = config_group('config_type_list');
        $group = config_group('config_group_list');
        return view('info', ['type' => $type, 'group' => $group]);
    }
    /**
     * @title   保存配置
     * @desc    保存配置
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   varchar 字段 &nbsp; 字段名 NO
     * @param   int 字段 &nbsp; 字段名 YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function save()
    {
        $config = new ConfigModel();
        if (request()->isPost()) {
            $param = input('post.');
            $config->info($param);
        }
    }
    /**
     * @title   配置状态切换
     * @desc    配置状态切换
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   int id &nbsp; ID YES
     * @param   int status &nbsp; 禁用还是启用 YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function status()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $status = input('status');
            if (empty($id)) {
                return tocode(100, 'ID不能为空');
            }
            if (empty($status)) {
                $status = 0;
            }
            $config = new ConfigModel();
            $status = $config->status($id, $status);
            if (!$status) {
                return tocode(100, '修改状态失败');
            }
            return tocode(200, '修改状态成功！');
        }
    }

    /**
     * @title   获取某个标签的配置参数
     * @desc    获取某个标签的配置参数
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   varchar 字段 &nbsp; 字段名 NO
     * @param   int id &nbsp; ID YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function group()
    {
        if (request()->isAjax()) {
            if (request()->isPost()) {
                $config = input('post.');
                $conf = new ConfigModel();
                return $conf->configSave($config);
            }
            $group = input('group_id');
            $list = ConfigModel::where(['status' => 1, 'group' => $group])
                ->field('id,name,group,title,extra,value,remark,type')
                ->order('sort')
                ->append(['type_text', 'group_text', 'image'])
                ->select();
            return todata($list);
        }
        $groups = ConfigModel::where('name', 'config_group_list')->field('type,value')->find();
        $group = self::parse($groups['type'], $groups['value']);
        unset($group[3]);
        return view('', ['group' => $group]);
    }
    /**
     * 根据配置类型解析配置
     * @param  integer $type  配置类型
     * @param  string  $value 配置值
     */
    private static function parse($type, $value)
    {
        switch ($type) {
            case 3: //解析数组
                $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
                if (strpos($value, ':')) {
                    $value = array();
                    foreach ($array as $val) {
                        list($k, $v) = explode(':', $val);
                        $value[$k] = $v;
                    }
                } else {
                    $value = $array;
                }
                break;
        }
        return $value;
    }
    public function delAll()
    {
        $ids = input('id');
        if (empty($ids)) {
            return tocode(100, 'ID不能为空');
        }
        $config = new ConfigModel();
        $all = $config->handleAll($ids, 2);
        if (!$all) {
            return tocode(100, '删除失败');
        }
        return tocode(200, '删除成功！');
    }
    /**
     * @title   批量操作
     * @desc    批量禁用、启用、删除
     * @author  橘子味的心
     * @version 1.0
     * @method  POST
     * @param   int type &nbsp; 类型 YES
     * @param   int id &nbsp; ID YES
     * @return  int code &nbsp; 返回参数 200/100
     * @return  string message &nbsp; 返回信息 successful
     * @return  array data &nbsp; 返回数据 successful
     */
    public function handleAll()
    {
        $ids = input('id');
        $type = input('type', 0);
        if (empty($ids)) {
            return tocode(100, 'ID不能为空');
        }
        if (empty($type)) {
            $type = 0;
        }
        $config = new ConfigModel();
        $all = $config->handleAll($ids, $type);
        switch ($type) {
            case '1':
                $name = '启用';
                break;
            case '2':
                $name = '删除';
                break;
            default:
                $name = '禁用';
                break;
        }
        if (!$all) {
            return tocode(100, $name . '失败');
        }
        return tocode(200, $name . '成功！');
    }
}
